﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TravelWeb.Models;

namespace TravelWeb.Controllers
{
    [Authorize]
    public class OrderController : Controller
    {
        private TravelWebDB db = new TravelWebDB();

        //
        // GET: /Order/

        public ViewResult Index()
        {
            List<Order> list = new List<Order>();
            var orders = db.Orders.Include(o => o.Customer).Include(o => o.Tour).Include(o => o.Schedule).Include(o => o.Promotion);
            foreach (var cus in db.Customers)
            {
                if (cus.Username.Equals(User.Identity.Name))
                {
                    foreach (var item in orders)
                    {
                        if (item.CustomerID == cus.CustomerId)
                            list.Add(item);
                    }
                    return View(list);
                }
            }
            return View(orders.ToList());
        }

        //
        // GET: /Order/Details/5

        public ViewResult Details(int id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // GET: /Order/Create

        public ActionResult Create(OrderKey key)
        {
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "FirstName" + " " + "LastName");
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName");
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName");
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName");
            Order order = new Order()
            {
                TourID = key.TourID,
                ScheduleID = key.ScheduleID,
                PromotionID = key.PromotionID,
                OrderDate = DateTime.Now,
                Status = "New"
            };
            return View(order);
        }

        //
        // POST: /Order/Create

        [HttpPost]
        public ActionResult Create(Order order)
        {
            if (ModelState.IsValid)
            {
                foreach (var bus in db.Transportations)
                {
                    if (bus.TransportationID == order.Schedule.TransportationID)
                    {
                        if (bus.numOfSeatAvailable >= (order.numOfAdult + order.numOfChild))
                        {
                            bus.numOfSeatAvailable = bus.numOfSeatAvailable - order.numOfAdult - order.numOfChild;
                        }
                    }
                }

                double adultPrice = order.Tour.AdultPrice * order.numOfAdult;
                double childPrice = order.Tour.ChildPrice * order.numOfChild;
                order.TotalPrice = adultPrice + childPrice;

                db.Orders.Add(order);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "Username", order.CustomerID);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", order.TourID);
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName", order.ScheduleID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", order.PromotionID);
            return View(order);
        }

        //
        // GET: /Order/Edit/5

        public ActionResult Edit(int id)
        {
            Order order = db.Orders.Find(id);
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "Username", order.CustomerID);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", order.TourID);
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName", order.ScheduleID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", order.PromotionID);
            return View(order);
        }

        //
        // POST: /Order/Edit/5

        [HttpPost]
        public ActionResult Edit(Order order)
        {
            if (ModelState.IsValid)
            {
                db.Entry(order).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "Username", order.CustomerID);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", order.TourID);
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName", order.ScheduleID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", order.PromotionID);
            return View(order);
        }

        //
        // GET: /Order/Delete/5

        public ActionResult Delete(int id)
        {
            Order order = db.Orders.Find(id);
            return View(order);
        }

        //
        // POST: /Order/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Order order = db.Orders.Find(id);
            //db.Orders.Remove(order);
            order.Status = "Canceled";
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Approve(int id)
        {
            Order order = db.Orders.Find(id);
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "Username", order.CustomerID);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", order.TourID);
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName", order.ScheduleID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", order.PromotionID);
            order.Status = "Approved";
            order.Schedule.Transportation.numOfSeatAvailable = order.Schedule.Transportation.numOfSeatAvailable - 
                order.numOfAdult - order.numOfChild;
            order.Schedule.numOfCustomer = order.Schedule.numOfCustomer + order.numOfAdult + order.numOfChild;
            db.SaveChanges();
            return View(order);
        }

        public ActionResult Confirm(int id)
        {
            Order order = db.Orders.Find(id);
            ViewBag.CustomerID = new SelectList(db.Customers, "CustomerId", "Username", order.CustomerID);
            ViewBag.TourID = new SelectList(db.Tours, "TourID", "TourName", order.TourID);
            ViewBag.ScheduleID = new SelectList(db.Schedules, "ScheduleID", "ScheduleName", order.ScheduleID);
            ViewBag.PromotionID = new SelectList(db.Promotions, "PromotionID", "PromotionName", order.PromotionID);
            order.TotalPrice = order.numOfAdult * order.Tour.AdultPrice + order.numOfChild * order.Tour.ChildPrice;
            order.TotalPrice = order.TotalPrice - (order.TotalPrice * order.Promotion.DiscountPercent);
            order.OrderDate = DateTime.Now;
            order.Status = "Confirmed";
            db.SaveChanges();
            return View(order);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}